<template>
  <div class="app-appNewsCategory-container">
    <el-card shadow="hover">
      <div class="app-appNewsCategory-search mb15">
        <el-form :model="tableData.param" ref="queryRef" :inline="true" label-width="100px">
          <el-row>
            <el-col :span="8" class="colBlock">
              <el-form-item label="ID" prop="id">
                <el-input v-model="tableData.param.id" placeholder="请输入ID" clearable
                  @keyup.enter.native="appNewsCategoryList" />
              </el-form-item>
            </el-col>
            <el-col :span="8" class="colBlock">
              <el-form-item label="名称" prop="cateName">
                <el-input v-model="tableData.param.cateName" placeholder="请输入名称" clearable
                  @keyup.enter.native="appNewsCategoryList" />
              </el-form-item>
            </el-col>
            <el-col :span="8" class="colBlock">
              <el-form-item>
                <el-button type="primary" @click="appNewsCategoryList"><el-icon><ele-Search /></el-icon>搜索</el-button>
                <el-button @click="resetQuery(queryRef)"><el-icon><ele-Refresh /></el-icon>重置</el-button>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" @click="handleAdd"
              v-auth="'api/v1/app/appNewsCategory/add'"><el-icon><ele-Plus /></el-icon>新增</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="success" :disabled="single" @click="handleUpdate(null)"
              v-auth="'api/v1/app/appNewsCategory/edit'"><el-icon><ele-Edit /></el-icon>修改</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" :disabled="multiple" @click="handleDelete(null)"
              v-auth="'api/v1/app/appNewsCategory/delete'"><el-icon><ele-Delete /></el-icon>删除</el-button>
          </el-col>
        </el-row>
      </div>
      <el-table v-loading="loading" :data="tableData.data" @selection-change="handleSelectionChange" row-key="id"
        default-expand-all :tree-props="{ children: 'children', hasChildren: 'hasChildren' }">
        <el-table-column type="selection" width="55" align="center" />        
        <el-table-column label="名称" prop="cateName" min-width="150px" />
        <el-table-column label="ID" align="center" prop="id" width="150px" />
        <el-table-column label="操作" align="center" class-name="small-padding" width="160px" fixed="right">
          <template #default="scope">
            <el-button type="primary" link @click="handleUpdate(scope.row)"
              v-auth="'api/v1/app/appNewsCategory/edit'"><el-icon><ele-EditPen /></el-icon>修改</el-button>
            <el-button type="primary" link @click="handleDelete(scope.row)"
              v-auth="'api/v1/app/appNewsCategory/delete'"><el-icon><ele-DeleteFilled /></el-icon>删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <ApiV1AppAppNewsCategoryEdit ref="editRef" :appNewsCategoryOptions="tableData.data"
      @appNewsCategoryList="appNewsCategoryList"></ApiV1AppAppNewsCategoryEdit>
    <ApiV1AppAppNewsCategoryDetail ref="detailRef" :appNewsCategoryOptions="tableData.data"
      @appNewsCategoryList="appNewsCategoryList"></ApiV1AppAppNewsCategoryDetail>
  </div>
</template>
<script lang="ts">
import { ItemOptions } from "/@/api/items";
import { toRefs, reactive, onMounted, ref, defineComponent, computed, getCurrentInstance, toRaw } from 'vue';
import { ElMessageBox, ElMessage, FormInstance } from 'element-plus';
import {
  listAppNewsCategory,
  getAppNewsCategory,
  delAppNewsCategory,
  addAppNewsCategory,
  updateAppNewsCategory,
} from "/@/api/app/appNewsCategory";
import {
  AppNewsCategoryTableColumns,
  AppNewsCategoryInfoData,
  AppNewsCategoryTableDataState,
} from "/@/views/app/appNewsCategory/list/component/model"
import ApiV1AppAppNewsCategoryEdit from "/@/views/app/appNewsCategory/list/component/edit.vue"
import ApiV1AppAppNewsCategoryDetail from "/@/views/app/appNewsCategory/list/component/detail.vue"
import _ from 'lodash'
export default defineComponent({
  name: "apiV1AppAppNewsCategoryList",
  components: {
    ApiV1AppAppNewsCategoryEdit,
    ApiV1AppAppNewsCategoryDetail
  },
  setup() {
    const { proxy } = <any>getCurrentInstance()
    const loading = ref(false)
    const queryRef = ref()
    const editRef = ref();
    const detailRef = ref();
    // 是否显示所有搜索选项
    const showAll = ref(false)
    // 非单个禁用
    const single = ref(true)
    // 非多个禁用
    const multiple = ref(true)
    const word = computed(() => {
      if (showAll.value === false) {
        //对文字进行处理
        return "展开搜索";
      } else {
        return "收起搜索";
      }
    })
    // 字典选项数据        
    const {
    } = proxy.useDict(
      )
    const dataList = ref<Array<AppNewsCategoryTableColumns>>([])
    const state = reactive<AppNewsCategoryTableDataState>({
      ids: [],
      tableData: {
        data: [],
        total: 0,
        loading: false,
        param: {
          pageNum: 1,
          pageSize: 10,
          id: undefined,
          cateName: undefined,
          dateRange: []
        },
      },
    });
    // 页面加载时
    onMounted(() => {
      initTableData();
    });
    // 初始化表格数据
    const initTableData = () => {
      appNewsCategoryList()
    };
    /** 重置按钮操作 */
    const resetQuery = (formEl: FormInstance | undefined) => {
      if (!formEl) return
      formEl.resetFields()
      appNewsCategoryList()
    };
    // 获取列表数据
    const appNewsCategoryList = () => {
      loading.value = true
      listAppNewsCategory(state.tableData.param).then((res: any) => {
        let list = res.data.list ?? [];
        dataList.value = _.clone(res.data.list)
        list = proxy.handleTree(res.data.list || [], "id", "pid");
        state.tableData.data = list;
        loading.value = false
      })
    };
    const toggleSearch = () => {
      showAll.value = !showAll.value;
    }
    // 多选框选中数据
    const handleSelectionChange = (selection: Array<AppNewsCategoryInfoData>) => {
      state.ids = selection.map(item => item.id)
      single.value = selection.length != 1
      multiple.value = !selection.length
    }
    const handleAdd = () => {
      editRef.value.openDialog()
    }
    const handleUpdate = (row: AppNewsCategoryTableColumns) => {
      if (!row) {
        row = dataList.value.find((item: AppNewsCategoryTableColumns) => {
          return item.id === state.ids[0]
        }) as AppNewsCategoryTableColumns
      }
      editRef.value.openDialog(toRaw(row));
    };
    const handleDelete = (row: AppNewsCategoryTableColumns) => {
      let msg = '你确定要删除所选数据？';
      let id: number[] = [];
      if (row) {
        msg = `此操作将永久删除数据，是否继续?`
        id = [row.id]
      } else {
        id = state.ids
      }
      if (id.length === 0) {
        ElMessage.error('请选择要删除的数据。');
        return
      }
      ElMessageBox.confirm(msg, '提示', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          delAppNewsCategory(id).then(() => {
            ElMessage.success('删除成功');
            appNewsCategoryList();
          })
        })
        .catch(() => { });
    }
    const handleView = (row: AppNewsCategoryTableColumns) => {
      detailRef.value.openDialog(toRaw(row));
    }
    return {
      proxy,
      editRef,
      detailRef,
      showAll,
      loading,
      single,
      multiple,
      word,
      queryRef,
      resetQuery,
      appNewsCategoryList,
      toggleSearch,
      handleSelectionChange,
      handleAdd,
      handleUpdate,
      handleDelete,
      ...toRefs(state),
    }
  }
})
</script>
<style lang="scss" scoped>
.colBlock {
  display: block;
}

.colNone {
  display: none;
}
</style>